/**
 * Created with IntelliJ IDEA.
 * User: Jeremy Schulte
 * Date: 11/15/13
 * Time: 7:17 PM
 */


import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

@SuppressWarnings("serial")
public class CustomizeGUI extends JFrame{
    private final int WINDOW_WIDTH = 200;
    private final int WINDOW_HEIGHT = 400;

    private JLabel taskLbl1;
    private JLabel taskLbl2;
    private JLabel taskLbl3;
    private JLabel taskLbl4;

    private JTextField taskFld1;
    private JTextField taskFld2;
    private JTextField taskFld3;
    private JTextField taskFld4;



    private JButton okButton;
    private JButton cancelButton;

    private JPanel customizePanel;

    // For debug purposes.
    private static boolean DEBUG = false;


    public CustomizeGUI() {
        super("Labels");

        //Window min size.
        setMinimumSize(new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT));

        //Frame image.
        BufferedImage image = null;
        try {
            image = ImageIO.read(
                    getClass().getResource("/Images/icon.png"));
        } catch (IOException e) {
            if(DEBUG)
                e.printStackTrace();
        }
        setIconImage(image);

        //Build the panel.
        Build();

        add(customizePanel, BorderLayout.CENTER);



    }

    public void Build() {

        //Panel
        customizePanel = new JPanel();


        //Layout
        customizePanel.setLayout(new GridLayout(0,2,0,60));


        //Labels and TextFields.
        taskLbl1 = new JLabel("Label 1");
        customizePanel.add(taskLbl1);
        taskFld1 = new JTextField();
        customizePanel.add(taskFld1);
        taskLbl2 = new JLabel("Label 2");
        customizePanel.add(taskLbl2);
        taskFld2 = new JTextField();
        customizePanel.add(taskFld2);
        taskLbl3 = new JLabel("Label 3");
        customizePanel.add(taskLbl3);
        taskFld3 = new JTextField();
        customizePanel.add(taskFld3);
        taskLbl4 = new JLabel("Label 4");
        customizePanel.add(taskLbl4);
        taskFld4 = new JTextField();
        customizePanel.add(taskFld4);

        //Buttons.
        okButton = new JButton("Ok");
        okButton.addActionListener(new ButtonListener());
        customizePanel.add(okButton);
        cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(new ButtonListener());
        customizePanel.add(cancelButton);







    }

    public class ButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == okButton) {

                File customFile = new File(".prefs");
                try {
                    FileWriter customWriter = new FileWriter(customFile);
                    customWriter.write("[Forms]\n");
                    if (taskFld1.getText().isEmpty()) {
                        customWriter.write("Forms\n");
                        PracticeGUI.resultsTable.getColumnModel().getColumn(1).setHeaderValue("Forms");
                        PracticeGUI.formsLabel.setText("Forms");


                    }

                    else {
                        customWriter.write(taskFld1.getText() +"\n");
                        PracticeGUI.resultsTable.getColumnModel().getColumn(1).setHeaderValue(taskFld1.getText());
                        PracticeControl.setForms = taskFld1.getText();
                        PracticeGUI.formsLabel.setText(taskFld1.getText());

                    }

                    customWriter.write("[Steps]\n");
                    if (taskFld2.getText().isEmpty()) {
                        customWriter.write("One-Step\n");
                        PracticeGUI.resultsTable.getColumnModel().getColumn(2).setHeaderValue("One-Steps");
                        PracticeGUI.stepsLabel.setText("One-Steps");
                    }

                    else {
                        customWriter.write(taskFld2.getText() +"\n");
                        PracticeGUI.resultsTable.getColumnModel().getColumn(2).setHeaderValue(taskFld2.getText());
                        PracticeGUI.stepsLabel.setText(taskFld2.getText());
                    }

                    customWriter.write("[Knowledge]\n");
                    if (taskFld3.getText().isEmpty()) {
                        customWriter.write("Knowledge\n");
                        PracticeGUI.resultsTable.getColumnModel().getColumn(3).setHeaderValue("Knowledge");
                        PracticeGUI.knowledgeLabel.setText("Knowledge");

                    }

                    else {
                        customWriter.write(taskFld3.getText() +"\n");
                        PracticeGUI.resultsTable.getColumnModel().getColumn(3).setHeaderValue(taskFld3.getText());
                        PracticeGUI.knowledgeLabel.setText(taskFld3.getText());



                    }

                    customWriter.write("[Other]\n");
                    if (taskFld4.getText().isEmpty()) {
                        customWriter.write("Other\n");
                        PracticeGUI.resultsTable.getColumnModel().getColumn(4).setHeaderValue("Other");
                        PracticeGUI.otherLabel.setText("Other");

                    }

                    else {
                        customWriter.write(taskFld4.getText() +"\n");
                        PracticeGUI.resultsTable.getColumnModel().getColumn(4).setHeaderValue(taskFld4.getText());
                        PracticeGUI.otherLabel.setText(taskFld4.getText());

                    }

                    customWriter.close();
                    dispose();

                } catch (IOException e1) {
                    if(DEBUG)
                        e1.printStackTrace();
                }

            }
            else if (e.getSource() == cancelButton) {
                dispose();

            }

        }

    }






}
